import 'package:eleapp/controller/usercontroller.dart';
import 'package:eleapp/res/strings.dart';
import 'package:eleapp/util/save/sp_util.dart';
import 'package:eleapp/util/theme_util.dart' as cus_theme;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

// import 'language_setting_controller.dart';
class ThemeSettingPage extends StatefulWidget {
  const ThemeSettingPage({super.key});

  @override
  State<ThemeSettingPage> createState() => _ThemeSettingPageState();
}

class _ThemeSettingPageState extends State<ThemeSettingPage> {
  int curThemeIndex = 0;
  // 新增主题要在此处手动增加
  var themeList = [
    cus_theme.Theme(StringStyles.followerSystemTheme.tr, ThemeIndex.Syetem),
    cus_theme.Theme(StringStyles.lightTheme.tr, ThemeIndex.Light),
    cus_theme.Theme(StringStyles.darkTheme.tr, ThemeIndex.Dark),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // 利用城市字符串判断当前语言的勾选
    curThemeIndex = SpUtil.getThemeIndex();
  }

  @override
  Widget build(BuildContext context) {
    SystemController systemController = Get.find();
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          StringStyles.themeSetting.tr,
        ),
        backgroundColor: Colors.blue,
      ),
      body: Container(
        padding: const EdgeInsets.only(
          top: 10,
        ),
        child: ListView.separated(
            itemBuilder: (context, index) {
              return Container(
                padding: const EdgeInsets.only(left: 16, right: 16),
                // color: Colors.white,
                height: 50,
                child: InkWell(
                  onTap: () {
                    print(themeList[index].mode);
                    // 系统语言的切换
                    cus_theme.ThemeOptions.updateLocale(
                        context, themeList[index].mode);
                    // 语言类型缓存到本地
                    SpUtil.updateThemeIndex(themeList[index].mode);
                    // 更新勾选图标位置
                    setState(() {
                      curThemeIndex = index;
                    });

                    // 更新主题提示词
                    systemController.updateThemeName(index);
                    // 返回上一页面 没有的话“随系统设置语言”自动切换语言不生效
                    Get.back(result: index);
                  },
                  child: Row(
                    children: [
                      Expanded(child: Text(themeList[index].name)),
                      Visibility(
                          // 利用城市字符串判断当前语言的勾选
                          visible: curThemeIndex == index,
                          child: const Icon(Icons.check_rounded,
                              color: Colors.blue))
                    ],
                  ),
                ),
              );
            },
            separatorBuilder: (context, index) {
              return Container(
                height: 1,
                color: Colors.black26,
              );
            },
            itemCount: themeList.length),
      ),
    );
  }
}
